{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c21ff0fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Image dimensions: 32 x 32 (width x height)\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "from PIL import Image\n",
    "\n",
    "# Path to the image\n",
    "image_path = 'cifar_1.png'\n",
    "\n",
    "# Check if the file exists\n",
    "if os.path.exists(image_path):\n",
    "    img = Image.open(image_path)\n",
    "    width, height = img.size\n",
    "    print(f\"Image dimensions: {width} x {height} (width x height)\")\n",
    "else:\n",
    "    print(f\"File not found: {image_path}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d33e0a0c",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_2729951/1950911838.py:23: UserWarning: Glyph 21407 (\\N{CJK UNIFIED IDEOGRAPH-539F}) missing from font(s) DejaVu Sans.\n",
      "  plt.tight_layout()\n",
      "/tmp/ipykernel_2729951/1950911838.py:23: UserWarning: Glyph 22987 (\\N{CJK UNIFIED IDEOGRAPH-59CB}) missing from font(s) DejaVu Sans.\n",
      "  plt.tight_layout()\n",
      "/tmp/ipykernel_2729951/1950911838.py:23: UserWarning: Glyph 25289 (\\N{CJK UNIFIED IDEOGRAPH-62C9}) missing from font(s) DejaVu Sans.\n",
      "  plt.tight_layout()\n",
      "/tmp/ipykernel_2729951/1950911838.py:23: UserWarning: Glyph 20280 (\\N{CJK UNIFIED IDEOGRAPH-4F38}) missing from font(s) DejaVu Sans.\n",
      "  plt.tight_layout()\n",
      "/tmp/ipykernel_2729951/1950911838.py:23: UserWarning: Glyph 21518 (\\N{CJK UNIFIED IDEOGRAPH-540E}) missing from font(s) DejaVu Sans.\n",
      "  plt.tight_layout()\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAjkAAAErCAYAAAA8HZJgAAAAOnRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjEwLjMsIGh0dHBzOi8vbWF0cGxvdGxpYi5vcmcvZiW1igAAAAlwSFlzAAAPYQAAD2EBqD+naQAAE0tJREFUeJzt3XuQl3W9wPHPIgos4IXQMi8oSqJ4SA01khBrXEYUQwS8YDKYGhajMZaUSV6yM0RE1MHsTIbYgBUkN7UJgbiUYJqmE0WjjFGEUCAsstyWy3P+4LCHdS8sCAf88HrN7B883+8+v++Pge+89/k9v9+WFEVRBABAMo0O9gIAAA4EkQMApCRyAICURA4AkJLIAQBSEjkAQEoiBwBISeQAACmJHAAgJZEDAKQkcg4zmzZtis997nNx7rnnxjHHHBMtWrSIj370o/H9738/tm7dWm3u7Nmz45ZbbomPfOQjUVpaGm3bto1bb701VqxYsd/WM2TIkLjggguiVatWUVpaGmeffXY88MADUVFRUW3eSy+9FIMHD44OHTpE8+bN49RTT41+/frF66+/vt/WAhw4y5YtiwcffDAuuuiiOO6446J169bRrVu3mDVrVo25+7r3lJeXxwknnBAlJSXxy1/+cr+su6Fr2bhxYzzyyCNRVlYWJ554YrRs2TLOP//8ePTRR2P79u31PsaECROipKQkWrRosV/WzP8p8burDg1//vOf4/zzz4+jjjqq1vHKyspYvHhxbN68uUHzzjjjjFrH16xZEz169IiuXbvGaaedFo0aNYoFCxbE+PHj4/rrr48nn3yyam6nTp1izZo10bdv32jXrl28+eabMWbMmCgtLY1XX301PvShD73n592lS5f42Mc+FmeeeWY0bdo0/vjHP8bYsWOjU6dOMX/+/GjUaGeH9+nTJ55//vno27dvdOzYMVauXBljxoyJioqKeOGFF+Lcc899z2uBTPb3nvJe954xY8bEPffcE7169YpLLrkktm3bFj/96U/jlVdeibFjx8bAgQOr5u7r3nPnnXfG2LFjY8OGDTFp0qTo06dPfX9FDdLQtSxatCg6duwYn/70p6OsrCyOPvromDFjRkyZMiVuvvnmeOKJJ2o9f0VFRZx11lmxbt26qj+zHxUcEv70pz8Vl1xySZ3jF198cfHGG280eN7eGjx4cBERxYoVK6qOzZs3r9i+fXu1efPmzSsiovj6179e4xyTJk0q1q5dW+v5V6xYUTz99NMNWsvIkSOLiCgWLlxYdez5558vtmzZUm3e66+/XjRp0qTo379/g84Lh5P9vae8171n0aJFxapVq6od27x5c9G+ffvi5JNPrnZ8b/eeotj5fBs3blw89NBDRUQUkyZNqjFnzZo1xVNPPVXnGp988smioqJin9ayatWqYtGiRTXOOXDgwCIi6vy7GTp0aHHWWWcV/fv3L5o3b17n2tg3Xq4iIiJOO+20iNh5uXeXrl27Vl1J2f1Yq1atYvHixdWOr1+/Pm6//fa46qqrYuPGjdXGysvLo3v37jFo0KCorKzcp7V84hOfqPETZLt27aJDhw411gIcejp06BCtW7eudqxJkybRo0eP+Oc//xnr16+vOr43e88ud911V1xzzTXxyU9+ss41/PCHP4x+/frFlClTaoz95Cc/iRtvvLHGFZeGrqV169bRoUOHGue95pprIiJqXfcbb7wR3/ve92LUqFHRuHHjOtfNvhM5h6nKyspYvXp1LFu2LKZMmRIjR46MNm3axJlnnlnv91VUVERFRUWNzaply5YxderUePnll6NPnz5V9/ds3Lgxrrrqqli6dGlMnz691kvd27Zti9WrV8dbb70Vzz33XNx3333RsmXLuOiii+pdS1EU8a9//avGWoD3j5UrV0ZpaWmUlpbWO6+uvSciYtKkSbFgwYIYMWJEvecYOnRoXHnllXHDDTfE7Nmzq45Pnjw5Pv/5z8dNN90Ud9xxxx7XXN9a3m3lypUREbXO/dKXvhSXXXZZ9OjRY4/nYd+InMPU5MmT4/jjj49TTz01evfuHSeffHI8/fTTe/xpYvTo0VFZWRnXXXddjbGuXbvGxIkTY+bMmfHZz342tmzZEn369ImXX345pk+fHhdccEGt5/zDH/4Qxx9/fJx00knRvXv3KIoipk+fHq1atap3LRMmTIjly5fXuhbg0LdkyZKYPHlyXHvttXHEEUfUO7euvWfTpk3x5S9/OYYMGVJ1FbgujRs3jl/84hfx8Y9/PHr16hW///3vY/bs2XHjjTfGFVdcEY8//niUlJTscd317YO7q6ysjNGjR8fpp58eF154YbWxZ599Np577rkYNWrUHh+Pfef62GHqsssui5kzZ0Z5eXnMnj07XnvttdiwYUO93zN//vx48MEHo1+/fvGpT32q1jk9e/aMsWPHxoABA2LhwoXx1ltvxVNPPRWXXnppnec955xzYubMmbFhw4ZYsGBBzJo1a4833/31r3+NL37xi9G5c+cYMGDAnp8wcEjZuHFj9O3bN5o1axbDhw+vd259e8/w4cNj69atce+99zbocZs2bRrTp0+vuoJSWVkZF198cUycOLFBLxk1ZB/cZfDgwfGXv/wlnn322WrnrqysjCFDhsSgQYPinHPOadC62UcH+6YgdjrYNx5/61vfKlq0aFHtxuPdLV68uGjVqlVx3nnnFe+8884ez9e1a9ciIoqrr756r9cyYcKEolGjRsWrr75a6/iKFSuKtm3bFqecckqxfPnyvT4/HA4OtRuPd7dt27aiZ8+exVFHHVXMnj273rn17T1/+9vfimbNmhVjx46tOjZnzpw6bzze3W9+85siIoqIKF555ZUGrXtv9sERI0YUEVF885vfrDE2fPjw4rjjjivefvvtqmMDBgxw4/EB4OUqImLnW7QrKipi2rRpNcaWLVsWZWVlccwxx8SvfvWraNmyZb3nGjp0aMyfPz/Kyspi+vTpe/wp7d169+4dERE///nPa4ytW7currjiiigvL49f//rX8eEPf3ivzg0cfLfddls888wzMW7cuHqvhuxp7/nGN74RJ510UnTr1i2WLl0aS5curboHZtWqVbF06dLYsWNHjfO++eab0b9//2jfvn20adMmrr322j1+Bs/e7IPjxo2LoUOHxqBBg+K+++6rNrZu3bp4+OGH47bbbot33nmnat0VFRVRFEUsXbo0/v3vf9e7FhrOy1VExM7XtSOi6rMadnn77bejrKwstmzZErNnz44TTzyx3vN8+9vfjhEjRsRDDz0Uw4YNiyFDhsTXvva1aNWqVdx+++0NWsuWLVtix44dNdayefPm6NmzZ7z++usxa9Ysl3nhfegrX/lKPP744zF69Oi44YYb6pzXkL3nH//4RyxZsiTatm1bY+wLX/hCRESsXbs2jj322KrjK1asiMsvvzyOPPLImDlzZmzatCm6dOkSZWVlMW/evFrvBdybfXDatGlx6623Ru/eveORRx6pMb527dqoqKiIESNG1Hqj9Omnnx6f+cxnYurUqXU+Bg0ncg4zq1evjg984AM1bq577LHHImLnB1/tsmHDhujRo0csX7485syZE+3atav33D/+8Y/jq1/9agwZMiSGDRsWERGjRo2K8vLyuOOOO+LYY4+Nfv36Vc0vLy+P5s2bx5FHHrnHtWzfvj2uu+66WLhwYUybNi06d+68D88eOJi+853vxMiRI+Pee++Nu+66q855Dd17Hn744Vi9enW1Y4sWLYphw4bFPffcE507d47mzZtXja1duza6d+8eFRUV8bvf/S5OPvnkiIiYMWNGdOvWLa688sqYNWtWte/Zm31w/vz5cf3110fXrl1jwoQJNd56HhFxwgkn1PoW9h/84AexcOHC+NnPfrbHHyZpOJFzmBk/fnz86Ec/il69ekXbtm1j/fr1MWPGjJg5c2b07Nmz2qXj/v37x4svvhi33HJLLF68uNrnPLRo0SJ69epV9ef169fH0KFDY+DAgfHd73636nhJSUk89thjUV5eHnfffXf06tWr6m3kc+fOjTvvvDP69OkT7dq1i8rKyvjtb38bkydPjk6dOsVNN91UdZ677747pk+fHj179ow1a9bE+PHjqz2v3ecCh54pU6bEPffcE+3atYuzzz67xv/hyy+/PD74wQ9GRMP3ni5dutR4nF1XbS688MJqe1TEzs/JWbZsWcydO7darJx33nnxzDPPRFlZWTzxxBNVV4H2Zi1///vf4+qrr46SkpLo06dPTJo0qdpjd+zYMTp27BilpaU11hURMXXq1HjxxRdrHeM9ONg3BbHT/9fNfy+99FLRt2/f4tRTTy2aNGlSNG/evLjggguKUaNGFVu3bq02t02bNlU35r37q02bNrU+h23bttX6uJs3by4WL15c7diSJUuKm2++uWjbtm3RrFmzomnTpkWHDh2K+++/v8anjl566aV1rsU/Y6jpULvx+P7776/3//CcOXOq5u7t3rO7+m483rp1a62fSrzLa6+9VuzYsaPasYauZdfj1vV1//3317tuNx4fGK7kHGY6deoUEydObNDcpUuX7tW56/v9UU2aNIn27dtXO3bGGWfU+ftc3m3u3Ll7tRb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RAwCkJHIAgJREDgCQksgBAFISOQBASiIHAEhJ5AAAKYkcACAlkQMApCRyAICURA4AkJLIAQBSEjkAQEoiBwBISeQAACmJHAAgJZEDAKQkcgCAlEQOAJCSyAEAUhI5AEBKIgcASEnkAAApiRwAICWRAwCkJHIAgJREDgCQksgBAFISOQBASiIHAEhJ5AAAKYkcACAlkQMApCRyAICURA4AkJLIAQBSEjkAQEoiBwBISeQAACmJHAAgJZEDAKQkcgCAlEQOAJCSyAEAUhI5AEBKIgcASEnkAAApiRwAICWRAwCkJHIAgJREDgCQksgBAFISOQBASiIHAEhJ5AAAKYkcACAlkQMApCRyAICURA4AkJLIAQBSEjkAQEoiBwBISeQAACmJHAAgJZEDAKQkcgCAlEQOAJCSyAEAUhI5AEBKIgcASEnkAAApiRwAICWRAwCkJHIAgJREDgCQksgBAFISOQBASiIHAEhJ5AAAKYkcACAlkQMApCRyAICURA4AkJLIAQBSEjkAQEoiBwBISeQAACmJHAAgJZEDAKQkcgCAlEQOAJCSyAEAUhI5AEBKIgcASEnkAAApiRwAICWRAwCkJHIAgJREDgCQksgBAFISOQBASiIHAEhJ5AAAKYkcACAlkQMApCRyAICURA4AkJLIAQBSEjkAQEoiBwBISeQAACmJHAAgJZEDAKQkcgCAlEQOAJCSyAEAUhI5AEBKIgcASEnkAAApiRwAICWRAwCkJHIAgJREDgCQksgBAFISOQBASiIHAEhJ5AAAKYkcACAlkQMApCRyAICURA4AkJLIAQBSEjkAQEoiBwBISeQAACmJHAAgJZEDAKQkcgCAlEQOAJCSyAEAUhI5AEBKIgcASEnkAAApiRwAICWRAwCkJHIAgJREDgCQksgBAFISOQBASiIHAEhJ5AAAKYkcACAlkQMApCRyAICURA4AkJLIAQBSEjkAQEolRVEUB3sRAAD7mys5AEBKIgcASEnkAAApiRwAICWRAwCkJHIAgJREDgCQksgBAFISOQBASv8Dl5mLEjODi1QAAAAASUVORK5CYII=",
      "text/plain": [
       "<Figure size 600x300 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from PIL import Image\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# 修改为你的实际路径，如果脚本与 cifar_1.png 在同一路径下，可直接用 \"./cifar_1.png\"\n",
    "path = \"cifar_1.png\"\n",
    "\n",
    "# 1. 加载原始 32×32 图片\n",
    "img = Image.open(path)\n",
    "\n",
    "# 2. 最近邻插值放大到 224×224\n",
    "resized = img.resize((224, 224), resample=Image.NEAREST)\n",
    "\n",
    "# 3. 可视化原图与拉伸后图像\n",
    "fig, axes = plt.subplots(1, 2, figsize=(6, 3))\n",
    "axes[0].imshow(img)\n",
    "axes[0].set_title(\"原始 32×32\")\n",
    "axes[0].axis(\"off\")\n",
    "\n",
    "axes[1].imshow(resized)\n",
    "axes[1].set_title(\"拉伸后 224×224\")\n",
    "axes[1].axis(\"off\")\n",
    "\n",
    "plt.tight_layout()\n",
    "plt.show()\n",
    "resized.save(\"cifar_3.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "5a67e436",
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "\n",
    "# 打开图片\n",
    "with Image.open(\"cifar_1_224.png\") as img:\n",
    "    # 获取图片尺寸\n",
    "    width, height = img.size\n",
    "    \n",
    "    # 定义左上角小块的尺寸（例如8x8）\n",
    "    box_size = 64\n",
    "    box = (0, 0, box_size, box_size)  # 左上角小块的坐标\n",
    "    \n",
    "    # 裁剪出左上角的小块\n",
    "    cropped_box = img.crop(box)\n",
    "    \n",
    "    # 放大裁剪出的小块（例如放大4倍）\n",
    "    resized_box = cropped_box.resize((box_size*2, box_size*2), resample=Image.NEAREST)\n",
    "    \n",
    "    # 创建一个新的画布\n",
    "    new_img = Image.new('RGB', (width, height))\n",
    "    \n",
    "    # 将放大后的小块放在中央位置\n",
    "    new_img.paste(resized_box, (width//2 - (box_size*4)//2, height//2 - (box_size*4)//2))\n",
    "    \n",
    "    # 保存修改后的图片\n",
    "    new_img.save(\"cifar_1_224_expansion.png\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b782b143",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "(eog:2812631): Gtk-WARNING **: 19:27:11.047: cannot open display: \n",
      "\n",
      "(eog:2812635): Gtk-WARNING **: 19:27:11.050: cannot open display: \n"
     ]
    }
   ],
   "source": [
    "from PIL import Image\n",
    "\n",
    "# Load the 32×32 trigger image\n",
    "img = Image.open(\"cifar_1.png\")\n",
    "\n",
    "# Resize to 224×224 using nearest-neighbor interpolation\n",
    "resized = img.resize((224, 224), resample=Image.NEAREST)\n",
    "\n",
    "# Save the result\n",
    "resized.save(\"cifar_1_224.png\")\n",
    "\n",
    "# Show both images for comparison (optional)\n",
    "img.show(title=\"Original 32×32\")\n",
    "resized.show(title=\"Resized 224×224\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "backdoor",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
